import CustomError from "../utils/Error";
/*
* 微信接口promise化
* */
module.exports = {

    /*
    * 小程序登录
    * */
    login: function () {
        return new Promise(function (resolve, reject) {
            wx.login({
                success: function (res) {
                    resolve(res)
                },
                fail: function (err) {
                    reject(new CustomError(err, "Wx.login").parse())
                }
            })
        })
    },


    /*
    * 小程序日志
    * */
    getLogManager: function (type, value) {
        const logger = wx.getLogManager();
        switch (type) {
            case "debug":
                logger.debug(value);
                break
            case "info":
                logger.info(value);
                break
            case "log":
                logger.log(value);
                break
            case "warn":
                logger.warn(value);
                break
            default:
                break
        }
    },

    /*
    * 设置缓存
    * */
    setStorage: function (key, data) {
        return new Promise(function (resolve, reject) {
            wx.setStorage({
                key: key,
                data: data,
                success: function () {
                    resolve()
                },
                fail: function (err) {
                    reject(err)
                }
            })
        })
    },

    /*
    * 移除缓存
    * */
    removeStorage: function (key) {
        return new Promise(function (resolve, reject) {
            wx.removeStorage({
                key: key,
                success () {
                    resolve()
                },
                fail: function (err) {
                    reject(err)
                }
            })
        })
    },

    /*
    * 选择图片
    * 默认允许所有方式选择
    * 默认只允许选择一张图片
    * */
    chooseImage: function (data = {}) {
        return new Promise(function (resolve, reject) {
            let newData = Object.assign({
                count: 1,
                sizeType: ["compressed"],
                sourceType: ["album", "camera"]
            }, data)
            wx.chooseImage({
                ...newData,
                success (res) {
                    // tempFilePath可以作为img标签的src属性显示图片
                    console.log(res)
                    resolve(res.tempFilePaths)
                },
                fail (err) {
                    reject(err)
                },
            })
        })
    },

    /*
    * 从客户端会话选择文件
    * 默认只允许上传一份非图片视频以外的文件
    * */
    chooseMessageFile: function (data = {}) {
        return new Promise(function (resolve, reject) {
            let newData = Object.assign({
                count: 1,
                type: "file"
            }, data)
            wx.chooseMessageFile({
                ...newData,
                success (res) {
                    resolve(res.tempFiles)
                },
                fail (err) {
                    reject(err)
                }
            })
        })
    },

    /*
    * 上传文件
    * */
    uploadFile: function (data = {}) {
        return new Promise(function (resolve, reject) {
            wx.uploadFile({
                ...data,
                success (res){
                    let data = JSON.parse(res.data);
                    if (data.code === 200) {
                        resolve(data.data)
                    } else {
                        reject(data)
                    }
                },
                fail (err) {
                    reject(err)
                }
            })
        })
    },

    /*
    * 下载文件
    * */
    downloadFile: function (data = {}) {
        return new Promise(function (resolve, reject) {
            wx.downloadFile({
                ...data,
                success:function(res){
                    console.log(res)
                    // let header = res.header["Content-Disposition"];
                    // let suffix = header.split(".")[1];
                    resolve(res.tempFilePath)
                },
                fail:function (err) {
                    reject(err)
                }
            })
        })
    },

    /*
    * 打开文件
    * */
    openDocument: function (filePath, fileType) {
        return new Promise(function (resolve, reject) {
            wx.openDocument({
                filePath: filePath,
                showMenu: true,
                success:function(res){
                    resolve(res)
                },
                fail:function (err) {
                    reject(err)
                }
            })
        })
    },

    /*
    * 获取配置项
    * */
    getSetting: function (name) {
        if (!name) {throw new Error("getSetting授权名称不能为空")}
        return new Promise(function (resolve, reject) {
            wx.getSetting({
                success (res) {
                    if (res.authSetting[name] || !(name in res.authSetting)) {
                        resolve(true)
                    } else {
                        resolve(false) //用户已拒绝授权
                    }
                },
                fail: function (err) {
                    reject(new CustomError(err, "Wx.getSetting").parse())
                }
            })
        })
    },

    /*
    * 获取定位信息
    * */
    getLocation: function () {
        return new Promise(function (resolve, reject) {
            wx.getLocation({
                type: "wgs84",
                isHighAccuracy: true,
                success: function (res) {
                    resolve(res)
                },
                fail: function (err) {
                    if (err.errMsg === "getLocation:fail system permission denied") { //用户未开启定位
                        reject(new CustomError("请前往设置--打开手机定位服务", "Wx.getLocation"))
                    } else if (err.errMsg === "getLocation:fail auth deny") { //用户拒绝授权
                        resolve(false);
                    } else {
                        reject(new CustomError(err, "Wx.getLocation"))
                    }
                }
            })
        })
    },

    /*
    * 使用微信内置地图查看位置
    * */
    openLocation: function (data = {}) {
        return new Promise(function (resolve, reject) {
            wx.openLocation({
                ...data,
                success: function () {
                    resolve()
                },
                fail: function (err) {
                    reject(err)
                }
            })
        })
    },
    
    /*
    * 剪切板 剪切
    * */
    setClipboardData: function (data) {
        return new Promise(function (resolve, reject) {
            wx.setClipboardData({
                data: data,
                success: function (res) {
                    resolve(res.data)
                },
                fail: function (err) {
                    reject(new CustomError(err, "Wx.setClipboardData"))
                }
            })
        })
    },

    /*
    * 剪切板 获取
    * */
    getClipboardData: function () {
        return new Promise(function (resolve, reject) {
            wx.getClipboardData({
                success: function (res) {
                    resolve(res.data)
                },
                fail: function (err) {
                    reject(new CustomError(err, "Wx.getClipboardData"))
                }
            })
        })
    },

    /*
    * 消息订阅
    * */
    requestSubscribeMessage: function (list = []) {
        return new Promise(function (resolve) {
            if (wx.requestSubscribeMessage) {
                wx.requestSubscribeMessage({
                    tmplIds: list,
                    fail: function (err) {
                        console.error(new CustomError(err, "Wx.requestSubscribeMessage").parse())
                    },
                    complete: function (res) {
                        resolve(res)
                    }
                })
            } else {
                resolve()
            }
        })
    },

    /*
    * 富文本编辑器api -- setContents
    * */
    editorSetContents: function (context, data = {}) {
        return new Promise(function (resolve, reject) {
            if (!context) {
                reject(new CustomError("editor 对象不存在", "Wx.editorSetContents"));
                return false
            }
            context.setContents({
                ...data,
                success: function (res) {
                    resolve(res)
                },
                fail: function (err) {
                    reject(err)
                }
            })
        })
    },
    
    /*
    * 富文本编辑器api -- getContents
    * */
    editorGetContents: function (context) {
        return new Promise(function (resolve, reject) {
            if (!context) {
                reject(new CustomError("editor 对象不存在", "Wx.editorGetContents"));
                return false
            }
            context.getContents({
                success: function (res) {
                    resolve(res)
                },
                fail: function (err) {
                    reject(err)
                }
            })
        })
    },

    /*
    * 拨打电话号码
    * */
    makePhoneCall: function (phoneNumber) {
        return new Promise(function (resolve, reject) {
            if (!phoneNumber) {
                reject(new CustomError("phoneNumber 对象不存在", "Wx.makePhoneCall"));
                return false
            }
            wx.makePhoneCall({
                phoneNumber: phoneNumber,
                success: function (res) {
                    resolve(res)
                },
                fail: function (err) {
                    reject(err)
                }
            })
        })
    }

}